package com.ruicar.afs.cloud.product.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.bizcommon.dto.rules.atom.AfsRuleCopyDto;
import com.ruicar.afs.cloud.bizcommon.rules.service.AfsRuleInfoService;
import com.ruicar.afs.cloud.bizcommon.rules.service.impl.AfsRuleInfoServiceImpl;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.product.entity.ProductRuleInfo;
import com.ruicar.afs.cloud.product.enums.FitScope;
import com.ruicar.afs.cloud.product.enums.RuleGroupEnum;
import com.ruicar.afs.cloud.product.enums.StatusEnum;
import com.ruicar.afs.cloud.product.enums.StatusType;
import com.ruicar.afs.cloud.product.mapper.ProductRuleInfoMapper;
import com.ruicar.afs.cloud.product.service.ProductRuleInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author yaorui.zhang
 * @date 22020年5月14日11:03:49
 */
@Service
public class ProductRuleInfoServiceImpl extends ServiceImpl<ProductRuleInfoMapper, ProductRuleInfo> implements ProductRuleInfoService {

    @Autowired
    private AfsRuleInfoServiceImpl afsRuleService;
    @Autowired
    private AfsRuleInfoService afsRuleInfoService;

    /**
     * 根据方案ID和场景查询规则列表
     *
     * @param planId
     * @param fitScope
     * @return
     */
    public List<ProductRuleInfo> queryProductRuleInfoList(long planId, String fitScope) {
        List<ProductRuleInfo> list = this.list(Wrappers.<ProductRuleInfo>query().lambda()
                .eq(ProductRuleInfo::getFitScope, fitScope)
                .eq(ProductRuleInfo::getPlanId, String.valueOf(planId))
        );
        return list;
    }

    /**
     * 产品规则列表信息删除
     *
     * @param planId
     * @param fitScope
     * @return
     */
    public IResponse<Boolean> delProductRuleInfoList(long planId, String fitScope) {
        List<ProductRuleInfo> list = (List<ProductRuleInfo>) this.queryProductRuleInfoList(planId, fitScope);
        for (ProductRuleInfo productRuleInfo : list) {
            this.delProductRuleInfo(productRuleInfo);
        }
        return IResponse.success("操作成功！");
    }

    /**
     * 产品规则信息删除
     *
     * @param productRuleInfo
     * @return
     */
    public IResponse<Boolean> delProductRuleInfo(ProductRuleInfo productRuleInfo) {
        //规则ID不为空时处理
        Long ruleId = productRuleInfo.getRuleId();
        if (ruleId != null) {
            afsRuleService.deleteRule(ruleId);//规则删除
        }
        this.removeById(productRuleInfo.getId());
        return IResponse.success("操作成功！");
    }

    /**
     * 方案ID以及场景-规则生效
     *
     * @param planId
     * @param fitScope
     */
    public void effectiveRule(long planId, String fitScope,String name) {
        List<ProductRuleInfo> list = (List<ProductRuleInfo>) this.queryProductRuleInfoList(planId, fitScope);
        //方案规则校验
        if(list.size()==0){
            Assert.isTrue(false, "【" + name + "】方案没有录入规则,请正确填写规则内容！");
        }
        //修改规则列表状态
        for (ProductRuleInfo productRuleInfo : list) {
            this.effectiveRule(productRuleInfo);
        }
    }

    /**
     * 规则对象-规则生效
     *
     * @param productRuleInfo
     */
    public void effectiveRule(ProductRuleInfo productRuleInfo) {
        //规则ID不为空时处理
        Long ruleId = productRuleInfo.getRuleId();
        if (ruleId != null) {
            if (AfsEnumUtil.key(StatusEnum.YES).equals(productRuleInfo.getActiveStatus())) {//反激活
                afsRuleService.activeRule(ruleId);
            } else {//初始化，塞时间
                afsRuleService.changeRuleDate(ruleId, DateUtil.offsetDay(DateUtil.date(), -1), DateUtil.offsetMonth(DateUtil.date(), 99));
            }
        } else {
            Assert.isTrue(false, "【" + productRuleInfo.getRuleName() + "】规则未配置！");
        }
        productRuleInfo.setActiveStatus(AfsEnumUtil.key(StatusEnum.NO));//反激活
        productRuleInfo.setRuleStatus(AfsEnumUtil.key(StatusEnum.YES));
        this.updateById(productRuleInfo);
    }

    /**
     * 方案ID以及场景-规则失效
     *
     * @param planId
     * @param fitScope
     */
    public void invalid(long planId, String fitScope) {
        List<ProductRuleInfo> list = (List<ProductRuleInfo>) this.queryProductRuleInfoList(planId, fitScope);
        //修改规则列表状态
        for (ProductRuleInfo productRuleInfo : list) {
            this.invalid(productRuleInfo);
        }
    }

    /**
     * 规则对象-规则失效
     *
     * @param productRuleInfo
     */
    public void invalid(ProductRuleInfo productRuleInfo) {
        //规则ID不为空时处理
        Long ruleId = productRuleInfo.getRuleId();
        if (ruleId != null) {
            afsRuleService.deActiveRule(ruleId);//规则反激活
            productRuleInfo.setActiveStatus(AfsEnumUtil.key(StatusEnum.YES));//反激活
        }
        productRuleInfo.setRuleStatus(AfsEnumUtil.key(StatusEnum.NO));
        this.updateById(productRuleInfo);
    }

    //方案复制
    @Override
    public void copyRulePlan(Long oldPlanID, String newPlanID, String fitScope) {
        //查询原有规则列表
        List<ProductRuleInfo> ruleInfosOldList = this.list(Wrappers.<ProductRuleInfo>query().lambda()
                .eq(ProductRuleInfo::getPlanId, String.valueOf(oldPlanID))
                .eq(ProductRuleInfo::getFitScope, fitScope)
        );
        this.copyRuleList(ruleInfosOldList, fitScope, newPlanID);
    }

    //列表复制
    public void copyRuleList(List<ProductRuleInfo> ruleInfosOldList, String fitScope, String newPlanID) {
        //复制原有规则列表
        for (ProductRuleInfo productRuleInfo : ruleInfosOldList) {
            //复制规则列表
            productRuleInfo.setId(null);
            productRuleInfo.setPlanId(newPlanID);
            productRuleInfo.setRuleStatus(AfsEnumUtil.key(StatusType.INVALID));
            this.save(productRuleInfo);
            //单个复制
            this.copyRuleOne(productRuleInfo, fitScope);
        }
    }

    /**
     * 单个复制
     *
     * @param productRuleInfo
     * @param fitScope
     */
    public void copyRuleOne(ProductRuleInfo productRuleInfo, String fitScope) {
        //组装规则详情数据
        if (productRuleInfo.getRuleId() != null) {
            AfsRuleCopyDto afsRuleCopyDto = new AfsRuleCopyDto();
            afsRuleCopyDto.setSourceId(productRuleInfo.getRuleId());
            afsRuleCopyDto.setToRuleNo(productRuleInfo.getId().toString());
            afsRuleCopyDto.setToRuleName(productRuleInfo.getRuleName());
            if (AfsEnumUtil.key(FitScope.LAUNCHPLAN).equals(fitScope)) {
                afsRuleCopyDto.setToRuleGroup(AfsEnumUtil.key(RuleGroupEnum.LAUNCHPLAN));
            } else if (AfsEnumUtil.key(FitScope.RISKCONTROL).equals(fitScope)) {
                afsRuleCopyDto.setToRuleGroup(AfsEnumUtil.key(RuleGroupEnum.RISKCONTROL));
            } else {
                afsRuleCopyDto.setToRuleGroup(productRuleInfo.getPlanId());
            }
            //复制规则详情
            Long newID = afsRuleInfoService.copyRule(afsRuleCopyDto);
            //关联新的ID
            productRuleInfo.setRuleId(newID);
            this.updateById(productRuleInfo);
        }
    }


}
